package com.ms.ctl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RefreshScope // 实现配置的自动更新
@RequestMapping("/skill")
public class SkillCtl {
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${testStr}")
    private String testStr;

    @GetMapping("/test")
    public String test() {
        return testStr;
    }

    @GetMapping("/create")
    public void createOrder(){
        restTemplate.getForObject("", String.class);
    }

    @GetMapping("/stock")
    public void getStock() {
        // nacos中的服务名
        List<ServiceInstance> list = discoveryClient.getInstances("stock-srv");
        String host = "";

        for (ServiceInstance serviceInstance : list) {
            host = host + "" + serviceInstance.getHost();
        }

    }

}
